﻿//-----------------------------------------------------------------------
// <copyright file="AboutDialog.cs" company="In The Hand Ltd">
//     Copyright © 2014-15 In The Hand Ltd. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using Windows.ApplicationModel.Resources;
using Windows.UI;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

// The Content Dialog item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace InTheHand.UI.ApplicationSettings
{
    internal sealed partial class AboutDialog : ContentDialog
    {
        Color? existingStatusBarForegroundColor;
        double existingStatusBarBackgroundOpacity;

        public AboutDialog()
        {
            this.InitializeComponent();

            existingStatusBarForegroundColor = Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ForegroundColor;
            existingStatusBarBackgroundOpacity = Windows.UI.ViewManagement.StatusBar.GetForCurrentView().BackgroundOpacity;
            StatusBar sb = StatusBar.GetForCurrentView();
            sb.ForegroundColor = Colors.White;
            sb.BackgroundOpacity = 0.0;


            this.Closed += AboutDialog_Closed;
            ResourceLoader rl = ResourceLoader.GetForCurrentView("InTheHand.UI.ApplicationSettings.About/Resources");

            PublisherHyperlink.Content = rl.GetString("MoreFrom") + InTheHand.ApplicationModel.Package.Current.PublisherDisplayName;
             Color bgColor = InTheHand.ApplicationModel.Package.Current.BackgroundColor;
            if (bgColor != Colors.Transparent)
            {
                Color modColor = Color.FromArgb((byte)0xff, (byte)(bgColor.R/2), (byte)(bgColor.G/2), (byte)(bgColor.B/2));
                this.Background = new SolidColorBrush(modColor);
            }

            Windows.ApplicationModel.Store.LicenseInformation license = Windows.ApplicationModel.Store.CurrentApp.LicenseInformation;
            if (license.IsTrial)
            {
                IsPrimaryButtonEnabled = true;
                PrimaryButtonText = rl.GetString("Buy");
            }

            this.DataContext = InTheHand.ApplicationModel.Package.Current;
        }

        void AboutDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
        {
            StatusBar sb = StatusBar.GetForCurrentView();

            if(existingStatusBarForegroundColor.HasValue)
            {
                sb.ForegroundColor = existingStatusBarForegroundColor.Value;
            }
            else
            {
                sb.ForegroundColor = null;
            }

            sb.BackgroundOpacity = existingStatusBarBackgroundOpacity;
        }

        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            await InTheHand.ApplicationModel.Store.CurrentApp.RequestDetailsAsync();
        }

        private async void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            await InTheHand.ApplicationModel.Store.CurrentApp.RequestReviewAsync();
        }

        private async void PublisherButton_Click(object sender, RoutedEventArgs e)
        {
            await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:search?publisher=" + InTheHand.ApplicationModel.Package.Current.PublisherDisplayName));
        }
    }
}
